package com.bang.low.doc;

import com.alibaba.fastjson.JSONObject;
import com.bang.EsUtils;
import org.apache.http.StatusLine;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.ResponseException;
import org.elasticsearch.client.RestClient;

import java.io.IOException;

/**
 * @ClassName: DocumentApi
 * @Desc: TODO
 * @author: <Bang>
 * @Date: 2022/01/18 16:55
 * @Version: 1.0
 * @Modified By:
 */
public class DocumentApi {

    public static void main(String[] args) throws IOException {
        final JSONObject json = new JSONObject();
        json.put("id",2);
        json.put("name","zhangsan");
        createDocument("/abc/doc/1", json);

        json.put("id",3);
        updateDocument("/abc/doc/1",json);

        deleteDocument("/abc/doc/1");
    }

    private static void deleteDocument(String endpoint) throws IOException {
        final RestClient lowLevelClient = EsUtils.getLowLevelClient();
        final Request request = new Request("DELETE", endpoint);
        try {
            final Response response = lowLevelClient.performRequest(request);
            final StatusLine statusLine = response.getStatusLine();
            final int statusCode = statusLine.getStatusCode();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusCode);
            System.out.println("response:" + responseBody);


        } catch (ResponseException e) {
            final Response response = e.getResponse();
            final StatusLine statusLine = response.getStatusLine();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusLine.getStatusCode());
            System.out.println("response:" + responseBody);
        }
    }


    /**
     * @throws IOException
     *
     * {"_index":"abc","_type":"doc","_id":"1","_version":5,"result":"updated","_shards":{"total":2,"successful":2,"failed":0},"_seq_no":4,"_primary_term":1}
     */
    public static void createDocument(String endpoint, JSONObject json) throws IOException {
        final RestClient lowLevelClient = EsUtils.getLowLevelClient();
        final Request request = new Request("POST", endpoint);
        request.setEntity(new NStringEntity(json.toJSONString(), ContentType.APPLICATION_JSON));
        try {
            final Response response = lowLevelClient.performRequest(request);
            final StatusLine statusLine = response.getStatusLine();
            final int statusCode = statusLine.getStatusCode();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusCode);
            System.out.println("response:" + responseBody);


        } catch (ResponseException e) {
            final Response response = e.getResponse();
            final StatusLine statusLine = response.getStatusLine();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusLine.getStatusCode());
            System.out.println("response:" + responseBody);
        }
    }

    public static void updateDocument(String endpoint, JSONObject json) throws IOException {
        final RestClient lowLevelClient = EsUtils.getLowLevelClient();
        final Request request = new Request("POST", endpoint);
        request.setEntity(new NStringEntity(json.toJSONString(), ContentType.APPLICATION_JSON));
        try {
            final Response response = lowLevelClient.performRequest(request);
            final StatusLine statusLine = response.getStatusLine();
            final int statusCode = statusLine.getStatusCode();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusCode);
            System.out.println("response:" + responseBody);


        } catch (ResponseException e) {
            final Response response = e.getResponse();
            final StatusLine statusLine = response.getStatusLine();
            String responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("statusCode:" + statusLine.getStatusCode());
            System.out.println("response:" + responseBody);
        }
    }
}
